//创建event
var _customEvent = document.createEvent('Event');
_customEvent.initEvent('HtmlToContentEvent', true, true);

//promise对象的resolve函数，用于同步
var _resolveFunc;

//接收content返回的event，通知await
document.addEventListener('ContentToHtmlEvent', function () {
  var resultData = localStorage.getItem('RESULT');
  _resolveFunc(resultData);
});

//被html调用，向content发送event
function sendEventToContentJs(data, callback) {
  var promise = new Promise(function (resolve, reject) {
    _resolveFunc = resolve;
    var jsonData = JSON.stringify(data);
    //alert(jsonData);

    localStorage.setItem('PRN_FUNC', jsonData);
    document.dispatchEvent(_customEvent);
  });
  promise.then(function (value) {
    //alert(value);
    callback(JSON.parse(value));
  });
  return promise;
}

/******** sdk 函数 *************/

//PRN_GetDllVersion(char* pbuf, int* psize);
function PRN_GetDllVersion(callback) {
  return sendEventToContentJs({ func: 'PRN_GetDllVersion' }, callback);
}

//PRN_ConnectDriver(char* printerName);
function PRN_ConnectDriver(printerName, callback) {
  return sendEventToContentJs({ func: 'PRN_ConnectDriver', para1: printerName }, callback);
}

//PRN_ConnectUsbPort (char* portName, int readTimeOut);
function PRN_ConnectUsbPort(portName, timeout, callback) {
  return sendEventToContentJs({ func: 'PRN_ConnectUsbPort', para1: portName, para2: timeout }, callback);
}

//PRN_SendBuffer (char* data);
function PRN_SendBuffer(data, callback) {
  return sendEventToContentJs({ func: 'PRN_SendBuffer', para1: data }, callback);
}

//PRN_CloseConnect(void);
function PRN_CloseConnect(callback) {
  return sendEventToContentJs({ func: 'PRN_CloseConnect' }, callback);
}

//PRN_AddTextToLabel (char* textToPrint, int posX, int posY);
function PRN_AddTextToLabel(text, x, y, callback) {
  return sendEventToContentJs({ func: 'PRN_AddTextToLabel', para1: text, para2: x, para3: y }, callback);
}

//PRN_AddTextToLabelEx (char* textToPrint, char* fontName,int fontSize, int posX, int posY, int dir, int align);
function PRN_AddTextToLabelEx(textToPrint, fontName, fontSize, posX, posY, dir, align, callback) {
  return sendEventToContentJs(
    { func: 'PRN_AddTextToLabel', para1: textToPrint, para2: fontName, para3: fontSize, para4: posX, para5: posY, para6: dir, para7: align },
    callback,
  );
}

//PRN_IndBarcodeFont (char* fontName,int size, int slant, int offset);
function PRN_IndBarcodeFont(fontName, size, slant, offset, callback) {
  return sendEventToContentJs({ func: 'PRN_IndBarcodeFont', para1: fontName, para2: size, para3: slant, para4: offset }, callback);
}

//PRN_AddBarcodeToLabel (char* codeToPrint, char* codeType,
//int height, int posX, int posY, int dir,
//	int align, int enlargeWidth);
function PRN_AddBarcodeToLabel(codeToPrint, codeType, height, posX, posY, dir, align, enlargeWidth, callback) {
  return sendEventToContentJs(
    {
      func: 'PRN_AddBarcodeToLabel',
      para1: codeToPrint,
      para2: codeType,
      para3: height,
      para4: posX,
      para5: posY,
      para6: dir,
      para7: align,
      para8: enlargeWidth,
    },
    callback,
  );
}

// PRN_AddImageToLabel (char* imgFilePath, int posX, int posY);
function PRN_AddImageToLabel(imgFilePath, posX, posY, callback) {
  return sendEventToContentJs({ func: 'PRN_AddImageToLabel', para1: imgFilePath, para2: posX, para3: posY }, callback);
}

//PRN_AddImageToLabelEx (char* imgFilePath, int posX,	int posY, int dir, int align);
function PRN_AddImageToLabelEx(imgFilePath, posX, posY, dir, align, callback) {
  return sendEventToContentJs({ func: 'PRN_AddImageToLabelEx', para1: imgFilePath, para2: posX, para3: posY, para4: dir, para5: align }, callback);
}

//PRN_AddCommand (char* data);
function PRN_AddCommand(data, callback) {
  return sendEventToContentJs({ func: 'PRN_AddCommand', para1: data }, callback);
}

//PRN_ClearLabelBuffer(void);
function PRN_ClearLabelBuffer(callback) {
  return sendEventToContentJs({ func: 'PRN_ClearLabelBuffer' }, callback);
}

//PRN_PrintLabel ( int numOfCopies );
function PRN_PrintLabel(count, callback) {
  return sendEventToContentJs({ func: 'PRN_PrintLabel', para1: count }, callback);
}

//PRN_GetDriverStatus ( int* pStatus);
function PRN_GetDriverStatus(callback) {
  return sendEventToContentJs({ func: 'PRN_GetDriverStatus' }, callback);
}

//PRN_GetPrinterStatus ( int* pStatus);
function PRN_GetPrinterStatus(callback) {
  return sendEventToContentJs({ func: 'PRN_GetPrinterStatus' }, callback);
}

//PRN_GetInfoString(PRN_INFO_TYPE type, char* pbuf, int* psize);
function PRN_GetInfoString(type, callback) {
  return sendEventToContentJs({ func: 'PRN_GetInfoString', para1: type }, callback);
}

//PRN_GetInfoInt(PRN_INFO_TYPE type, int* pvalue);
function PRN_GetInfoInt(type, callback) {
  return sendEventToContentJs({ func: 'PRN_GetInfoInt', para1: type }, callback);
}

////PRN_SetCfg(PRN_CFG_ID id, int value);
function PRN_SetCfg(id, value, callback) {
  return sendEventToContentJs({ func: 'PRN_SetCfg', para1: id, para2: value }, callback);
}

//PRN_AddBarcodeToLabelEx(char* codeToPrint, char* codeType,	int height, int posX, int posY, int dir,		int align, int enlargeWidth, int *piAdv);
function PRN_AddBarcodeToLabelEx(codeToPrint, codeType, height, posX, posY, dir, align, enlargeWidth, iAdvArray, callback) {
  return sendEventToContentJs(
    {
      func: 'PRN_AddBarcodeToLabelEx',
      para1: codeToPrint,
      para2: codeType,
      para3: height,
      para4: posX,
      para5: posY,
      para6: dir,
      para7: align,
      para8: enlargeWidth,
      para9: iAdvArray,
    },
    callback,
  );
}

// const printBarCode = async (filteredArr) => {
//   // const filteredArr = dataSourceTable.filter((item) => selectRowKeys?.rowKeysList.includes(item.id));
//   console.log('filteredArr', filteredArr);
//   await PRN_GetDllVersion(function (returnData) {
//     console.log('PRN_GetDllVersionresult', returnData.result);
//     console.log('PRN_GetDllVersionpara', returnData.para);
//   });
//   // 连接打印机
//   await PRN_ConnectDriver('honeywell', 1000, function (returnData) {
//     console.log('连接成功', returnData.result);
//   });
//   for (let i = 0; i < filteredArr.length; i++) {
//     // 打印图片
//     await PRN_AddImageToLabel(
//       filteredArr[i].barCodeUrl, // 图片文件的路径
//       30, // 打印起点X坐标
//       270, // 打印起点Y坐标
//       function (returnData) {
//         console.log('打印图片', returnData.result);
//       },
//     );
//     // 执行打印，份数为 1 枚
//     await PRN_PrintLabel(1, function (returnData) {
//       console.log('执行打印份数', returnData.result);
//     });
//   }
//   // 查看打印机信息
//   // await PrintObj.PRN_GetInfoString(0, function (data) {
//   //   console.log('查看打印机信息PRN_GetInfoString', data.para);
//   // });
//   // 关闭连接
//   await PRN_CloseConnect(function (returnData) {
//     console.log('关闭连接', returnData.result);
//   });
// };

// export default printBarCode;

// export {
//   sendEventToContentJs,
//   PRN_GetDllVersion,
//   PRN_ConnectDriver,
//   PRN_ConnectUsbPort,
//   PRN_SendBuffer,
//   PRN_CloseConnect,
//   PRN_AddTextToLabel,
//   PRN_AddTextToLabelEx,
//   PRN_IndBarcodeFont,
//   PRN_AddBarcodeToLabel,
//   PRN_AddImageToLabel,
//   PRN_AddImageToLabelEx,
//   PRN_AddCommand,
//   PRN_ClearLabelBuffer,
//   PRN_PrintLabel,
//   PRN_GetDriverStatus,
//   PRN_GetPrinterStatus,
//   PRN_GetInfoString,
//   PRN_GetInfoInt,
//   PRN_SetCfg,
//   PRN_AddBarcodeToLabelEx,
// };
